﻿import QtQuick 2.0
import Box2D 2.0
import "./shared"
Item {
    id: root

    property var guankaItems: new Array

    function create(){
        Math.seed = getSeed()
        var num = Math.floor(Math.random() * 7) + 3

        for(var k in guankaItems){
            guankaItems[k].destroy()
        }
        guankaItems.length = 0

        console.log("num",num)
        var x = Math.random()*700 + 750
        for(var i=0;i<num;i++){
            if(i < 2){
                var stone1 = comstone.createObject(screen)
                stone1.x = x
                stone1.y = screen.height - ground.height - stone1.height*(i+1)
                guankaItems.push(stone1)
            }
            else{
                var wood1 = comwood.createObject(screen)
                wood1.y = screen.height - ground.height - wood1.height*(i+1)
                wood1.x = x
                guankaItems.push(wood1)
            }
        }
    }

    function addZero(value){
        if(value < 10){
            return "0" + value
        }
        return value
    }

    function getSeed(){

        var date = new Date
        var y = date.getFullYear()
        var M = addZero(date.getMonth())
        var d = addZero(date.getDate())
        var h = addZero(date.getHours())
        var m = addZero(date.getMinutes())
        var s = addZero(date.getSeconds())

        var time = String(y) + String(M) + String(d) + String(h) + String(m) + String(s)

        return Number(time)
    }

    Component{
        id: comwood

        ImgBody{
            id: wood
            objectName: "wood"
            source: "qrc:/imamges/image/wood.png"
            body.bodyType: Body.Dynamic

            fixtures: [
                Box {
                    friction: 1
                    density: 5
                    restitution: 0.2
                    width: wood.width
                    height: wood.height
                }
            ]
        }
    }

    Component{
        id: comstone

        ImgBody{
            id: stone
            objectName: "stone"
            source: "qrc:/imamges/image/stone.png"
            body.bodyType: Body.Dynamic

            fixtures: [
                Box {
                    friction: 1
                    density: 10
                    restitution: 0.0
                    width: stone.width
                    height: stone.height
                }
            ]
        }
    }

}
